package com.lensyn.generator.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.JavaBeansUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static org.mybatis.generator.internal.util.JavaBeansUtil.getCamelCaseString;

/**
 * @author xiaob@lensysn.com
 * @date 2016/12/19
 */
public class NameUtils {

    public static String name(IntrospectedTable introspectedTable, String format) {
        String tableName = getCamelCaseString(
                introspectedTable.getFullyQualifiedTable().getIntrospectedTableName(), true);
        return format.replaceAll("\\{name\\}", tableName);
    }


    public static Field logger(String classNAme) {
        Field logger = new Field("logger", new FullyQualifiedJavaType("org.slf4j.Logger"));
        logger.setVisibility(JavaVisibility.PROTECTED);
        logger.setStatic(true);
        logger.setFinal(true);
        logger.setInitializationString("LoggerFactory.getLogger(" + classNAme + ".class)");

        return logger;
    }

    public static Field autowiredField(CompilationUnit unit) {
        Field field = new Field(JavaBeansUtil.getValidPropertyName(unit.getType().getShortName()), unit.getType());
        field.setVisibility(JavaVisibility.PROTECTED);
        field.addAnnotation("@Autowired");
        return field;
    }


    public static void addClassComment(JavaElement element, IntrospectedTable introspectedTable, String addComment) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Properties properties = introspectedTable.getContext().getProperties();


        String remarks = introspectedTable.getRemarks();
        if (remarks == null) {
            remarks = "";
        }

        remarks += (addComment == null ? "" : addComment);


        element.addJavaDocLine("/**");
        element.addJavaDocLine(" * " + remarks);
        element.addJavaDocLine(" * @author " + properties.getProperty("@author", ""));
        element.addJavaDocLine(" * @date " + sdf.format(new Date()));
        element.addJavaDocLine(" * @since " + properties.getProperty("@since", properties.getProperty("@version", "")));
        element.addJavaDocLine(" * @version " + properties.getProperty("@version", properties.getProperty("@since", "")));
        element.addJavaDocLine(" */");
    }

    public static void addMethodComment(Method method, String addComment) {
        List<Parameter> parameters = method.getParameters();
        addComment = addComment == null ? "" : addComment;
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * " + addComment);
        for (Parameter parameter : parameters) {
            method.addJavaDocLine(" * @param " + parameter.getName());
        }
        if (method.getReturnType() != null) {
            method.addJavaDocLine(" * @return");
        }

        method.addJavaDocLine(" */");
    }


    /**
     * 复数生成规则
     * @param name
     * @return
     */
    public static String plural(String name){
        if(name ==null||name.trim().isEmpty()){
            return name;
        }

        name = name.trim();

        String result;

        //s，x，sh，ch或z
        if(name.endsWith("s")||name.endsWith("x")||name.endsWith("sh")||name.endsWith("ch")||name.endsWith("z")){
            return name+"es";
        }

        //y
        if(name.endsWith("y")){
            //a e i o u y
            if(name.endsWith("ay")||name.endsWith("ey")||name.endsWith("iy")||name.endsWith("oy")||name.endsWith("uy")){
                return name+"es";
            }
            if(name.length()==1){
                return name+"s";
            }

            //变y为i +es
            String temp = name.substring(0,name.length()-1);

            return temp+"ies";
        }

        //todo 还有些特殊的单词es

        //默认+s
        return name+"s";

    }

    /**
     * 驼峰转化成 key.key.key
     * eg: myBody = my.body
     * @param cameName
     * @return
     */
    public static String cameToPropertiesKey(String cameName){
       return cameToPropertiesKey(cameName,true);
    }
    /**
     * 驼峰转化成 key.key.key
     * eg: myBody = my.body
     * @param cameName
     * @return
     */
    public static String cameToPropertiesKey(String cameName,boolean endPoint){
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < cameName.length(); i++) {
            char c = cameName.charAt(i);

            if(Character.isUpperCase(c)&&i!=0){
                sb.append(".");
            }

            sb.append(Character.toLowerCase(c));
        }

        if(endPoint){
            sb.append(".");
        }

        return sb.toString();

    }


}
